package com.ruoyi.information.service.pc.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.PageUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.information.domain.pc.ServiceArticle;
import com.ruoyi.information.domain.pc.SysUser;
import com.ruoyi.information.mapper.pc.ServiceArticleMapper;
import com.ruoyi.information.mapper.pc.ServiceCenterMapper;
import com.ruoyi.information.mapper.pc.SysUserServiceMapper;
import com.ruoyi.information.service.pc.IServiceArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 服务资讯Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-17
 */
@Service
public class ServiceArticleServiceImpl implements IServiceArticleService {
    @Autowired
    private ServiceArticleMapper serviceArticleMapper;

    @Autowired
    private ServiceCenterMapper serviceCenterMapper;

    @Autowired
    private SysUserServiceMapper serviceMapper;

    /**
     * 查询服务资讯
     *
     * @param id 服务资讯主键
     * @return 服务资讯
     */
    @Override
    public ServiceArticle selectServiceArticleById(Long id) {
        /*增加浏览量*/
        serviceArticleMapper.updateNum(id);
        ServiceArticle serviceArticle = serviceArticleMapper.selectServiceArticleById(id);
        if (SecurityUtils.getUserId() != 0) {
            /*判断是否收藏*/
            serviceArticle.setIsLike(serviceArticleMapper.selectIsLike(id, SecurityUtils.getUserId()));
        }
        if (serviceArticle != null) {
            SysUser sysUser = new SysUser();
            sysUser.setDeptId(id);
            List<SysUser> sysUsers = serviceMapper.selectSysUserList(sysUser);
            //查询联系人
            if (sysUsers != null && !sysUsers.isEmpty()) {
                serviceArticle.setUserList(sysUsers);
            }
        }

        return serviceArticle;
    }

    /**
     * 查询服务资讯列表
     *
     * @param serviceArticle 服务资讯
     * @return 服务资讯
     */
    @Override
    public List<ServiceArticle> selectServiceArticleList(ServiceArticle serviceArticle) {

        //新多选
        if (serviceArticle.getIds() != null && !serviceArticle.getIds().isEmpty()) {
            List<Long> ids = new ArrayList<>();

            for (Long id : serviceArticle.getIds()) {
                //父查子
                ids.add(id);
                List<Long> idss = serviceCenterMapper.selectServiceCenterIdsById(id);
                if (idss != null && !idss.isEmpty()) {
                    ids.addAll(idss);
                }
                //子查父
                List<Long> idsp = serviceCenterMapper.selectServiceCenterParentIdsById(id);
                if (idsp != null && !idsp.isEmpty()) {
                    ids.addAll(idsp);
                }
            }

            serviceArticle.setIds(ids);

        } else {
            //旧单选
            if (serviceArticle.getServiceId() != null && !Objects.equals(serviceArticle.getServiceId(), "")) {
                List<Long> ids = new ArrayList<>();
                ids.add(Long.valueOf(serviceArticle.getServiceId()));
                //父查子
                List<Long> ids1 = serviceCenterMapper.selectServiceCenterIdsById(Long.valueOf(serviceArticle.getServiceId()));
                if (ids1 != null && !ids1.isEmpty()) {
                    ids.addAll(ids1);
                }
                //子查父
                List<Long> ids2 = serviceCenterMapper.selectServiceCenterParentIdsById(Long.valueOf(serviceArticle.getServiceId()));
                if (ids2 != null && !ids2.isEmpty()) {
                    ids.addAll(ids2);
                }
                serviceArticle.setServiceId(null);
                serviceArticle.setIds(ids);
            }
//            switch (String.valueOf(serviceCenter.getLevelNumber())) {
//                case "1": {
//                    List<Long> ids = new ArrayList<>();
//                    ids.add(serviceCenter.getId());
//
//                    ServiceCenter center = new ServiceCenter();
//                    center.setParentId(serviceCenter.getId());
//                    //2
//                    List<ServiceCenter> list = serviceCenterMapper.selectServiceCenterList(center);
//                    if (list != null && !list.isEmpty()) {
//                        for (ServiceCenter x : list) {
//                            ids.add(x.getId());
//                            center = new ServiceCenter();
//                            center.setParentId(serviceCenter.getId());
//                            //3
//                            List<ServiceCenter> xlist = serviceCenterMapper.selectServiceCenterList(center);
//                            if (xlist != null && !xlist.isEmpty()) {
//                                for (ServiceCenter o : xlist) {
//                                    ids.add(o.getId());
//                                }
//                            }
//                        }
//                    }
//                    serviceArticle.setIds(ids);
//                    break;
//                }
//                case "2": {
//                    List<Long> ids = new ArrayList<>();
//                    ids.add(serviceCenter.getId());
//                    ServiceCenter center = new ServiceCenter();
//                    center.setParentId(serviceCenter.getId());
//                    //3
//                    List<ServiceCenter> list = serviceCenterMapper.selectServiceCenterList(center);
//                    if (list != null && !list.isEmpty()) {
//                        for (ServiceCenter x : list) {
//                            ids.add(x.getId());
//                        }
//                    }
//                    serviceArticle.setIds(ids);
//                    break;
//                }
//                case "3":
//                    List<Long> ids = new ArrayList<>();
//                    ids.add(serviceCenter.getId());
//                    serviceArticle.setIds(ids);
//                    break;
//            }
        }
        PageUtils.startPage();
        return serviceArticleMapper.selectServiceArticleList(serviceArticle);
    }

    /**
     * 新增服务资讯
     *
     * @param serviceArticle 服务资讯
     * @return 结果
     */
    @Override
    public int insertServiceArticle(ServiceArticle serviceArticle) {
        serviceArticle.setCreateTime(DateUtils.getNowDate());
        return serviceArticleMapper.insertServiceArticle(serviceArticle);
    }

    /**
     * 修改服务资讯
     *
     * @param serviceArticle 服务资讯
     * @return 结果
     */
    @Override
    public int updateServiceArticle(ServiceArticle serviceArticle) {
        serviceArticle.setUpdateTime(DateUtils.getNowDate());
        return serviceArticleMapper.updateServiceArticle(serviceArticle);
    }

    /**
     * 批量删除服务资讯
     *
     * @param ids 需要删除的服务资讯主键
     * @return 结果
     */
    @Override
    public int deleteServiceArticleByIds(Long[] ids) {
        return serviceArticleMapper.deleteServiceArticleByIds(ids);
    }

    /**
     * 删除服务资讯信息
     *
     * @param id 服务资讯主键
     * @return 结果
     */
    @Override
    public int deleteServiceArticleById(Long id) {
        return serviceArticleMapper.deleteServiceArticleById(id);
    }
}
